import graphene
from flask import Flask
from flask_graphql import GraphQLView

class Animal(graphene.Interface):
    id = graphene.ID()
    name = graphene.String()

class Mouse(graphene.ObjectType):
    class Meta:
        interfaces =(Animal,)
    run = graphene.String()

class Bird(graphene.ObjectType):
    class Meta:
        interfaces =(Animal,)
    fly = graphene.String()

class Query(graphene.ObjectType):
    mouse = graphene.Field(Mouse)
    bird = graphene.Field(Bird)
    animal = graphene.Field(Animal,type_ = graphene.Int(required = True))
    def resolve_mouse(self,info):
        return {'id':1,'name':'杰瑞','run':'跑呀~Tom来了！！'}
    def resolve_bird(self,info):
        return {'id':2,'name':'鹦鹉','fly':'跑呀~Tom来了！！'}
    def resolve_animal(self,info,type_):
        # return Animal(id=3,name='动物') #  "message": "An Interface cannot be intitialized",
        if type_ == 1:
            return Mouse(id= 4, name='米老鼠',run='追唐老鸭啦~~~')
        else:
            return Bird(id = 3,name='鹦鹉',fly = '跑呀~Tom来了！！')

if __name__ =='__main__':
    schema = graphene.Schema(query= Query)
    app = Flask(__name__)
    app.add_url_rule('/graphql',view_func=GraphQLView.as_view('grapql',schema=schema, graphiql=True))
    app.run(debug=True)